#include <iostream>
#include "httplib.hpp"
#include "add_contacts.pb.h"

using namespace std;
using namespace httplib;

#define CONTACTS_HOST "47.120.23.210"
#define CONTACTS_PORT 8123

void menu()
{
    cout << "-------------------------------" << endl;
    cout << "请选择" << endl;
    cout << "1.新增联系人" << endl;
    cout << "2.删除联系人" << endl;
    cout << "3.查看联系人列表信息" << endl;
    cout << "4.查看联系人详细信息" << endl;
    cout << "0.退出" << endl;
    cout << "--------------------------------" << endl;
}

void buildAddContactRequest(add_contact::AddContactRequest* req)
{
    cout << "请输入联系人姓名" << endl;
    string name;
    getline(cin, name);
    req->set_name(name);

    cout << "请输入联系人年龄" << endl;
    int age;
    cin >> age;
    req->set_age(age);
    cin.ignore(256, '\n');

    for(int i=0; ;i++)
    {
        cout << "请输入联系人电话" << i+ 1 << "(输入回车完成新增):" << endl;
        string number;
        getline(cin, number);
        if(number.empty()) break;
        add_contact::AddContactRequest_Phone* phone = req->add_phone();
        phone->set_number(number);

        cout << "输入电话类型(1.移动电话 2.固定电话)" << endl;
        int type;
        cin >> type;
        cin.ignore(256, '\n');
        switch(type){
            case 1:
                phone->set_type(add_contact::AddContactRequest_Phone_PhoneType::AddContactRequest_Phone_PhoneType_MP);
                break;
            case 2:
                phone->set_type(add_contact::AddContactRequest_Phone_PhoneType::AddContactRequest_Phone_PhoneType_TEL);
                break;
            default:
                cout << "选择错误" << endl;
                break;
        }
    }
}

void addContact()
{
    Client cli(CONTACTS_HOST, CONTACTS_PORT);

    // 构造req
    add_contact::AddContactRequest req;
    buildAddContactRequest(&req);

    // 序列化req
    string req_str;
    if(!req.SerializeToString(&req_str))
    {
        cout << "序列化失败" << endl;
    }
    // 发起post调用
    auto res = cli.Post("/contacts/add", req_str, "application/protobuf");
    if(!res)
    {
        cout << "/contacts/add 连接失败" << endl;
    }
    // 反序列化resp
    add_contact::AddContactResponce resp;
    bool parse = resp.ParseFromString(res->body);
    if(res->status != 200 && !parse)
    {
        cout << "调用失败" << endl;
    }
    else if(!resp.success())
    {
        cout << "结果异常" << endl;
    }
    // 结果打印
    cout << "新增联系人成功, 联系人ID: " << resp.uid() << endl;
}

int main()
{
    while(true)
    {
        menu();
        int n;
        cin >> n;
        cin.ignore(256, '\n');
        switch(n){
            case 0:
                cout << "退出" << endl;
                return 0;
            case 1:
                addContact();
                break;
            case 2:
                break;
            case 3:
                break;
            default:
                cout << "重新选择" << endl;
                break;
        }
    }

    return 0;
}

// int main()
// {
//     Client cli(CONTACTS_HOST, CONTACTS_PORT);

//     Result res1 = cli.Post("/test-post");
//     if(res1->status == 200)
//     {
//         cout << "调用post成功" << endl;
//     }

//     Result res2 = cli.Get("/test-get");
//     if(res2->status == 200)
//     {
//         cout << "调用get成功" << endl;
//     }
//     return 0;
// }